package function;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @ClassName: MyPredicate
 * @Author: 王延
 * @Date: 2019/7/24 11:48
 * @Description :
 */
public class MyPredicate {
    public static void main(String[] args) {
        Predicate<String> predicate = (t) -> t.startsWith("p");
        System.out.println(predicate.test("predicate"));

        List<Double> list = Arrays.asList(11.2, 98.7, 45.6, 76.3, 65.2);
        //使用lambda 指定谓词判断规则
        List<Double> resluts = filterList(list, (Double d) -> {
            if (d >= 60) {
                return true;
            } else {
                return false;
            }
        });
        for (Double reslut : resluts) {
            System.out.println(reslut);
        }
    }

    public static <T> List<T> filterList(List<T> list, Predicate<T> predicate) {
        List<T> results = new LinkedList<>();
        //过滤>60  使用谓词过滤
        for (T t : list) {
            if (predicate.test(t)) {
                results.add(t);
            }
        }
        return results;
    }
}
